package com.qdlc.p2p.dal.mybatis.pager.support;

import com.qdlc.p2p.dal.mybatis.pager.core.PageSqlHandlerFactory;
import com.qdlc.p2p.dal.mybatis.pager.core.PageSqlHanlder;


/**
 * PageSqlHandlerFactory接口的默认实现类
 * @author linya
 *
 */
public class DefaultPageSqlHandlerFactory implements PageSqlHandlerFactory {

	public PageSqlHanlder factory(String dialect) {
		if (dialect == null || "".equals(dialect) || "mysql".equalsIgnoreCase(dialect)) {
			return new MySqlPageSqlHanlder();
		} else if ("oracle".equalsIgnoreCase(dialect)) {
			return new OraclePageSqlHandler();
		} else {
			try {
				// 试着去实例化
				Class<?> clazz = Class.forName(dialect);
				Object obj = clazz.newInstance();
				if (obj instanceof PageSqlHanlder) {
					return (PageSqlHanlder) obj;
				} else {
					throw new IllegalArgumentException("this class:" + dialect + " is not a subclass of PageSqlHanlder");
				}
			} catch (Exception e) {
				throw new IllegalArgumentException("this can not supprot this dialect:" + dialect + ". please implement a subclass of PageSqlHanlder");
			}
		}
	}

}
